package windows;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2019-06-23.
 */
public class HandleIniFile {

    private Pattern _section  = Pattern.compile( "\\s*\\[([^]]*)\\]\\s*" );
    private Pattern  _keyValue = Pattern.compile( "\\s*([^=]*)=(.*)" );
    private Map< String,
            Map< String,
                                String >>  _entries  = new HashMap<>();

    private  final  static  String path = "src/user.ini";
    private  final  static  String section_employee = "employee";


    public HandleIniFile () {
        try {
            load(  );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void load(  ) throws IOException {
        try( BufferedReader br = new BufferedReader( new FileReader( path ))) {
            String line;
            String section = null;
            while(( line = br.readLine()) != null ) {
                Matcher m = _section.matcher( line );
                if( m.matches()) {
                    section = m.group( 1 ).trim();
                }
                else if( section != null ) {
                    m = _keyValue.matcher( line );
                    if( m.matches()) {
                        String key   = m.group( 1 ).trim();
                        String value = m.group( 2 ).trim();
                        Map< String, String > kv = _entries.get( section );
                        if( kv == null ) {
                            _entries.put( section, kv = new HashMap<>());
                        }
                        kv.put( key, value );
                    }
                }
            }
        }
    }

    public String getString(   String key, String defaultvalue ) {
        Map< String, String > kv = _entries.get( section_employee );
        if( kv == null ) {
            return defaultvalue;
        }
        return kv.get( key );
    }
    public   String [] getSection() {
        Map< String, String > map = _entries.get( section_employee );
        Collection<String> valueCollection = map.values();
        final int size = valueCollection.size();

        List<String> valueList = new ArrayList<String>(valueCollection);

        String[] valueArray = new String[size];
        return  map.values().toArray(valueArray);
    }
    public void writeIniFile(String s){
        FileWriter fw = null;
        try {
             //如果文件存在，则追加内容；如果文件不存在，则创建文件
            File f=new File(path);
            fw = new FileWriter(f, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        pw.println(s);
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public  void setValue( String key, String value) {
        Map<String, String> sectionMap = _entries.get(section_employee);
        if (sectionMap == null) {
            sectionMap = new HashMap<String, String>();
            _entries.put(section_employee, sectionMap);
        }
        sectionMap.put(key, value);
    }
    public  void updateValue( String key, String value) {
        // Try to update values
        try {
            Properties pri = new Properties();
            pri.store(new FileOutputStream(path),key );
            pri.setProperty(key, value);

        }
        catch(IOException ioe) {
            System.out.println(ioe.getMessage());
        }
    }
}
